package cn.ffcs.uec.staff.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cn.ffcs.uec.common.util.PermissionUtil;
import cn.ffcs.uec.outStaff.vo.UecOutStaffTrialVo;
import cn.ffcs.uec.staff.dao.UecTrialDao;
import cn.ffcs.uec.staff.model.UecTrial;
import cn.ffcs.uom.common.constants.BaseUnitConstants;
import cn.ffcs.uom.common.dao.BaseDaoImpl;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.PageInfo;

@Repository("uecTrialDao")
@Transactional
public class UecTrialDaoImpl extends BaseDaoImpl implements UecTrialDao {

	@Override
	public PageInfo queryPageInfoByUecTrial(UecTrial uecTrial, int current, int pageSize) {
		if (null != uecTrial) {
			StringBuilder sb = new StringBuilder(" SELECT * FROM UEC_TRIAL T WHERE 1 = 1 ");
			List<Object> params = new ArrayList<Object>();
			sb.append(" AND T.STATUS_CD = ? ");
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);

			if (BaseUnitConstants.NM_OUT_STAFF_TYPE.equals(uecTrial.getNmStaffType())) {
				sb.append("  AND T.NM_STAFF_TYPE = ?  ");
				params.add(BaseUnitConstants.NM_OUT_STAFF_TYPE);
			}
			if (!StrUtil.isNullOrEmpty(uecTrial.getPkPsndoc())) {// 判断数据是否是集团的
				sb.append(" AND T.PK_PSNDOC = ? ");
				params.add(uecTrial.getPkPsndoc());
			} else if (!StrUtil.isNullOrEmpty(uecTrial.getStaffId())) {
				sb.append(" AND T.STAFF_ID = ? ");
				params.add(uecTrial.getStaffId());
			}
			return this.jdbcFindPageInfo(sb.toString(), params, current, pageSize, UecTrial.class);
		}
		return null;
	}

	@Override
	public UecTrial findUecTrial(UecTrial uecTrial) {
		if (null != uecTrial) {
			if (!StrUtil.isNullOrEmpty(uecTrial.getStaffId())) {
				StringBuilder sb = new StringBuilder(" SELECT * FROM UEC_TRIAL T WHERE T.STATUS_CD = ? ");
				List<Object> params = new ArrayList<Object>();
				params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
				if (!StrUtil.isNullOrEmpty(uecTrial.getNmStaffType())) {
					sb.append(" AND T.NM_STAFF_TYPE = 2 ");
				}
				sb.append(" AND T.STAFF_ID = ?  ");
				params.add(uecTrial.getStaffId());
				sb.append(" ORDER BY T.BEGINDATE desc");
				List<UecTrial> list = super.jdbcFindList(sb.toString(), params, UecTrial.class);
				if (list.size() > 0 && list != null) {
					return list.get(0);
				}
			}
		}
		return null;
	}

	@Override
	public UecTrial findUecTrialById(UecTrial uecTrial) {

		if (null != uecTrial) {
			StringBuilder sb = new StringBuilder(" SELECT * FROM UEC_TRIAL T WHERE  1=1 ");
			List<Object> params = new ArrayList<Object>();
			sb.append(" AND T.TRIAL_ID = ? AND T.NM_STAFF_TYPE = 2 ");
			params.add(uecTrial.getTrialId());
			List<UecTrial> list = super.jdbcFindList(sb.toString(), params, UecTrial.class);
			if (list.size() > 0 && list != null) {
				return list.get(0);
			}
		}
		return null;
	}

	@Override
	public PageInfo queryPageInfo(int current, int pageSize) {
		StringBuilder sqlStr = new StringBuilder("SELECT u.psncode,u.psnname,u.idnum,u.mobtelephone,"
				+ "t.trial_type,t.trialresult,t.begindate,t.enddate,t.regulardate"
				+ " FROM UEC_TRIAL T, UEC_OUT_STAFF_INFO U, UEC_ORG_ORGS S "
				+ "WHERE T.STATUS_CD = '1000' AND U.STATUS_CD = '1000' "
				+ "AND t.staff_id = u.out_staff_id AND t.nm_staff_type = '2' "
				+ "AND U.PK_ORG = S.PK_ORG AND to_char(to_date(T.ENDDATE,'yyyy-mm-dd'),'yyyy-mm-dd') <= TO_CHAR(TRUNC(SYSDATE), 'yyyy-mm-dd') "
				+ "AND T.TRIALRESULT != 1 AND T.TRIALRESULT != 3 AND S.Name LIKE ?");
		String org = PermissionUtil.getManagerOrg();
		List<Object> params = new ArrayList<Object>();
		/*if(!"区本部".equals(org)) {
			sqlStr.append(" AND S.Name LIKE ?");
		   params.add("%" + org + "%");
		}*/
		params.add("%" + org + "%");
		return this.jdbcFindPageInfo(sqlStr.toString(), params, current, pageSize, UecOutStaffTrialVo.class);
	}

	@Override
	public UecTrial findUecTrialOnApproval(UecTrial uecTrial) {

		if (null != uecTrial) {
			if (!StrUtil.isNullOrEmpty(uecTrial.getStaffId())) {
				StringBuilder sb = new StringBuilder(" SELECT * FROM UEC_TRIAL T WHERE  ");
				List<Object> params = new ArrayList<Object>();
				sb.append(" T.STATUS_CD = ? ");
				params.add(BaseUnitConstants.ENTT_STATE_PROCESS);
				sb.append(" AND T.STAFF_ID = ? AND T.NM_STAFF_TYPE = 2 ");
				params.add(uecTrial.getStaffId());

				List<UecTrial> list = super.jdbcFindList(sb.toString(), params, UecTrial.class);
				if (list.size() > 0 && list != null) {
					return list.get(0);
				}
			}
		}
		return null;

	}
}
